package com.cens.mall.coupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cens.mall.coupon.entity.SeckillSkuRelationEntity;
import com.cens.mall.coupon.service.SeckillSkuRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cens.common.utils.PageUtils;
import com.cens.common.utils.Query;

import com.cens.mall.coupon.dao.SeckillSessionDao;
import com.cens.mall.coupon.entity.SeckillSessionEntity;
import com.cens.mall.coupon.service.SeckillSessionService;


@Service("seckillSessionService")
public class SeckillSessionServiceImpl extends ServiceImpl<SeckillSessionDao, SeckillSessionEntity> implements SeckillSessionService {

    @Autowired
    private SeckillSkuRelationService seckillSkuRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SeckillSessionEntity> page = this.page(
                new Query<SeckillSessionEntity>().getPage(params),
                new QueryWrapper<SeckillSessionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<SeckillSessionEntity> getLatest3DaysSession()
    {
        LocalDate now = LocalDate.now();
        LocalDate plus2 = now.plusDays(2);
        LocalTime min = LocalTime.MIN;
        LocalTime max = LocalTime.MAX;
        LocalDateTime startTime = LocalDateTime.of(now, min);
        String startTimeFormat = startTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        LocalDateTime endTime = LocalDateTime.of(plus2, max);
        String endTimeFormat = endTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        List<SeckillSessionEntity> list = this.list(
                new LambdaQueryWrapper<SeckillSessionEntity>()
                        .between(SeckillSessionEntity::getStartTime, startTimeFormat, endTimeFormat)
        );
        if(list != null){
            return list.stream()
                    .peek(
                            session -> {
                                Long sessionId = session.getId();
                                List<SeckillSkuRelationEntity> seckillSkuRelationEntities = seckillSkuRelationService.list(
                                        new LambdaQueryWrapper<SeckillSkuRelationEntity>()
                                                .eq(SeckillSkuRelationEntity::getPromotionSessionId, sessionId)
                                );
                                session.setSkuRelationEntityList(seckillSkuRelationEntities);
                            }
                    ).collect(Collectors.toList());
        }
        return null;
    }

}